package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.Dept;
import com.example.sys.mapper.DeptMapper;
import com.example.sys.service.DeptService;
import com.example.sys.vo.DeptVo;
import com.example.sys.vo.IPageVo;
import com.example.util.BaseUtils;
import com.example.util.VoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/addDept")
    public ResponseBean<UnicomResponseEnums> addDept(@RequestBody @Valid Dept dept){
        if (!dept.toString().isEmpty()) {
            deptService.save(dept);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteDept")
    public ResponseBean<UnicomResponseEnums> deleteDept(Long deptId){
        if (!deptId.toString().isEmpty()) {
            deptService.removeById(deptId);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteByDeptId")
    public ResponseBean<UnicomResponseEnums> deleteByDeptId(@RequestBody DeptVo deptVo){
        if (!deptVo.getChildren().isEmpty()){
            return new ResponseBean(true, UnicomResponseEnums.HAVE_CHILDRENDEPT_NOT_DELETE);
        }
        if (deptService.removeById(deptVo.getDeptId())){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteDepts")
    public ResponseBean<UnicomResponseEnums> deleteDepts(List<Long> deptIds){
        if (!deptIds.isEmpty()) {
            for (Long deptid:deptIds) {
                deleteDept(deptid);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateDeptById")
    public ResponseBean<UnicomResponseEnums> updateDeptById(@RequestBody @Valid Dept dept){
        if (!dept.toString().isEmpty()) {
            deptService.updateById(dept);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/updateDepts")
    public ResponseBean<UnicomResponseEnums> updateDepts(@Valid List<Dept> depts){
        if (!depts.isEmpty()) {
            for (Dept dept:depts) {
                updateDeptById(dept);
            }
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @RequestMapping("/getDept")
    public ResponseBean<UnicomResponseEnums> getDept(Long deptId){
        if (!deptId.toString().isEmpty()) {
            Dept dept=deptService.getById(deptId);
            return new ResponseBean(true, dept, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @GetMapping("/getDeptList")
    public ResponseBean<UnicomResponseEnums> getDeptList(DeptVo deptVo){
        System.out.println(deptVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(deptVo, false, true);
        List<Dept> depts=deptService.list();
        BaseUtils baseUtils=new BaseUtils();
        if (stringObjectMap.isEmpty()){
            return new ResponseBean(true, depts, UnicomResponseEnums.SUCCESS_OPTION);
        }
        if (deptVo.getParentId()==null){
            List<DeptVo> list =baseUtils.ArrangementDeptTree(depts, 0L);
            return new ResponseBean(true, list, UnicomResponseEnums.SUCCESS_OPTION);
        }
        QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deptVo.getParentId());
        IPage<Dept> iPageDept=deptService.selectPage(deptVo);
        List<DeptVo> listDepts = new ArrayList<>();
        for (Dept d:iPageDept.getRecords()) {
            listDepts.add(VoUtil.deptToDeptVo(d));
        }
        IPageVo<DeptVo> iPageVo = new IPageVo<>();

        iPageVo.setSize(iPageDept.getSize());
        iPageVo.setCurrent(iPageDept.getCurrent());
        iPageVo.setTotal(iPageDept.getTotal());
        iPageVo.setPages(iPageDept.getPages());
        List<DeptVo> deptVoList=baseUtils.ArrangementDeptTree(iPageDept,depts);
        iPageVo.setRecords(deptVoList);
        return new ResponseBean(true, iPageVo, UnicomResponseEnums.SUCCESS_OPTION);



    }

}
